package net.seabookchen.controller;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.seabookchen.dao.BlogCommentDao;
import net.seabookchen.pojo.BlogComment;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.appengine.api.datastore.EntityNotFoundException;

@Controller
public class BlogCommentController extends CommonController<BlogComment> {
	private static final Logger log = Logger.getLogger(BlogCommentController.class.getName());
	private BlogCommentDao bcDao;
	
	public BlogCommentController() {
		if (bcDao == null) bcDao = new BlogCommentDao(BlogComment.class);
	}
	
	@RequestMapping(value ="/comment", method=RequestMethod.POST)
	public String postComment(HttpServletRequest request, HttpServletResponse resp, Model model) {
		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String url = request.getParameter("url");
		String content = request.getParameter("content");
		BlogComment comment = new BlogComment(username, email, url, content);
		bcDao.put(comment);
		populateModel(model, comment, RequestMethod.POST + "-BlogComment-" + comment.getId());
		log.info("Post BlogComment: " + comment);
		return "showComments";
	}
	
	@RequestMapping(value ="/allcomments", method=RequestMethod.GET)
	public String listComments(HttpServletRequest request, HttpServletResponse resp, Model model) {
		List<BlogComment> comments =  bcDao.listLatestComments();
		model.addAttribute("action", "List All Comments");
		model.addAttribute("entities", comments);
		log.info("List All Comments size: " + comments.size());
		return "showComments";
	}
	
	@RequestMapping(value ="/allcomments/{index}", method=RequestMethod.GET)
	public String listComments(HttpServletRequest request, HttpServletResponse resp, @PathVariable String index, Model model) {
		List<BlogComment> comments =  bcDao.listLatestComments(Integer.valueOf(index));
		//model.addAttribute("action", "List Latest " + index + " Comments");
		model.addAttribute("isBlogPage", false);
		model.addAttribute("entities", comments);
		log.info("List All Comments size: " + comments.size());
		return "showComments";
	}
	
	@RequestMapping(value ="/comment/{id}", method=RequestMethod.GET)
	public String getComment(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		BlogComment comment = null;
		try {
			comment = bcDao.get(Long.parseLong(id));
		} catch (NumberFormatException e) {
			log.log(Level.SEVERE, e.getMessage());
		} catch (EntityNotFoundException e) {
			log.log(Level.SEVERE, e.getMessage());
		}
		populateModel(model, comment, RequestMethod.GET + "-BlogComment-" +id);
		log.info("GET BlogComment: " + comment);
		return "showComments";
	}
	
	@RequestMapping(value ="/comment/{id}", method=RequestMethod.PUT)
	public String putComment(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String url = request.getParameter("url");
		String content = request.getParameter("content");
		BlogComment comment = null;
		try {
			comment = bcDao.get(Long.parseLong(id));
			comment.setUsername(username);
			comment.setEmail(email);
			comment.setUrl(url);
			comment.setContent(content);
			bcDao.put(comment);
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, comment, RequestMethod.GET + "-BlogComment-" +id);
		log.info("Put Comment: " + comment);
		return "showComments";
	}
	
	@RequestMapping(value ="/comment/{id}", method=RequestMethod.DELETE)
	public String deleteComment(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		bcDao.delete(Long.parseLong(id));
		model.addAttribute("action", RequestMethod.DELETE + "-BlogComment-" +id);
		log.info("Delete BlogComment: " + id);
		return "postSuccess";
	}
}
